{
 "metadata": {},
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Addressing Array Columns by Name\n",
      "================================\n",
      "\n",
      "There are two very closely related ways to access array columns by name:\n",
      "recarrays and structured arrays. Structured arrays are just ndarrays\n",
      "with a complicated data type:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python numbers=disable\n",
      "In [1]: from numpy import *\n",
      "In [2]: ones(3, dtype=dtype([('foo', int), ('bar', float)]))\n",
      "Out[2]:\n",
      "array([(1, 1.0), (1, 1.0), (1, 1.0)],\n",
      "      dtype=[('foo', '<i4'), ('bar', '<f8')])\n",
      "In [3]: r = _\n",
      "In [4]: r['foo']\n",
      "Out[4]: array([1, 1, 1])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "recarray is a subclass of ndarray that just adds attribute access to\n",
      "structured arrays:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python numbers=disable\n",
      "In [10]: r2 = r.view(recarray)\n",
      "In [11]: r2\n",
      "Out[11]:\n",
      "recarray([(1, 1.0), (1, 1.0), (1, 1.0)],\n",
      "      dtype=[('foo', '<i4'), ('bar', '<f8')])\n",
      "In [12]: r2.foo\n",
      "Out[12]: array([1, 1, 1])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "One downside of recarrays is that the attribute access feature slows\n",
      "down all field accesses, even the r['foo'] form, because it sticks a\n",
      "bunch of pure Python code in the middle. Much code won't notice this,\n",
      "but if you end up having to iterate over an array of records, this will\n",
      "be a hotspot for you.\n",
      "\n",
      "Structured arrays are sometimes confusingly called record arrays.\n",
      "\n",
      "`.\u00a0-\u00a0lightly\u00a0adapted\u00a0from\u00a0a\u00a0Robert\u00a0Kern\u00a0post\u00a0of\u00a0Thu,\u00a026\u00a0Jun\u00a02008\u00a015:25:11\u00a0-0500`\n",
      "\n",
      "Converting to regular arrays and reshaping\n",
      "==========================================\n",
      "\n",
      "A little script showing how to efficiently reformat structured arrays\n",
      "into normal ndarrays.\n",
      "\n",
      "Based on: [printing structured\n",
      "arrays](http://old.nabble.com/printing-structured-arrays-td27794203.html)."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python numbers=disable\n",
      "\n",
      "import numpy as np\n",
      "\n",
      "data = [ (1, 2), (3, 4.1), (13, 77) ]\n",
      "dtype = [('x', float), ('y', float)]\n",
      "\n",
      "print('\\n ndarray')\n",
      "nd = np.array(data)\n",
      "print nd\n",
      "\n",
      "print ('\\n structured array')\n",
      "\n",
      "struct_1dtype = np.array(data, dtype=dtype)\n",
      "print struct_1dtype\n",
      "\n",
      "print('\\n structured to ndarray')\n",
      "struct_1dtype_float = struct_1dtype.view(np.ndarray).reshape(len(struct_1dtype), -1) \n",
      "print struct_1dtype_float\n",
      "\n",
      "print('\\n structured to float: alternative ways')\n",
      "struct_1dtype_float_alt = struct_1dtype.view((np.float, len(struct_1dtype.dtype.names)))\n",
      "print struct_1dtype_float_alt\n",
      "\n",
      "# with heterogeneous dtype.\n",
      "struct_diffdtype = np.array([(1.0, 'string1', 2.0), (3.0, 'string2', 4.1)],\n",
      "dtype=[('x', float),('str_var', 'a7'),('y',float)])\n",
      "print('\\n structured array with different dtypes')\n",
      "print struct_diffdtype\n",
      "struct_diffdtype_nd = struct_diffdtype[['str_var', 'x', 'y']].view(np.ndarray).reshape(len(struct_diffdtype), -1) \n",
      "\n",
      "\n",
      "print('\\n structured array with different dtypes to reshaped ndarray')\n",
      "print struct_diffdtype_nd\n",
      "\n",
      "\n",
      "print('\\n structured array with different dtypes to reshaped float array ommiting string columns')\n",
      "struct_diffdtype_float = struct_diffdtype[['x', 'y']].view(float).reshape(len(struct_diffdtype),-1)\n",
      "print struct_diffdtype_float"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}